<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uji extends CI_controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('uji_model');
		$this->load->library('korelasi');
	}
   
	function data($id_kuesioner)
	{
		$this->load->model('kuesioner_model');
		$data_responden = array();
		$data['id_kuesioner'] = $id_kuesioner;
		$data['pertanyaan'] = $this->kuesioner_model->get_pertanyaan($id_kuesioner);
		$data['responden'] = $this->kuesioner_model->responden($id_kuesioner);
		foreach($data['responden'] as $responden)
		{
			$pertanyaan['data_jawaban'] = $this->kuesioner_model->get_jawaban($responden['fk_id_responden']);
			$data_responden[] = $pertanyaan;
		}
		$data['data_responden'] = $data_responden;
		//var_dump($data);
		$this->load->view('data_pengujian', $data);
	
	}
	
	function delete_responden()
	{
		//var_dump($_POST['id_responden']);
		$data = $this->uji_model->get_id_jawaban_responden($_POST['id_responden']);
		foreach($data as $jawaban)
		{
			$this->uji_model->delete_jawaban_responden($jawaban['fk_id_jawaban_responden']);
		}
		$this->uji_model->delete_responden($_POST['id_responden']);
	}
	
	function uji_data()
	{
		$this->load->view('uji_data');
	}
	
	function reliabilitas()
	{
		if(count($_POST['data'][0]) > 1)
		{
			$jml_item = 0;
			$jml_kuadrat = 0;
			$jumlah_varians = 0;

			for($a = 0; $a < count($_POST['data']); $a++)
			{
				for($b = 0; $b < count($_POST['data'][$a]); $b++)
				{
					$jml_item += $_POST['data'][$a][$b];
					$jml_kuadrat += pow($_POST['data'][$a][$b],2);
				}
				$jml_item1[$a] = $jml_item;
				$jml_kuadrat1[$a] = $jml_kuadrat;
				$jml_item = 0;
				$jml_kuadrat = 0;
			}
			
			for($c = 0; $c < count($_POST['data']); $c++)
			{
					$varians[$c] = ($jml_kuadrat1[$c]-(pow($jml_item1[$c],2)/count($_POST['data'][0])))/count($_POST['data'][0]);
			}
			
			for($d = 0; $d < count($_POST['data']); $d++)
			{
				$jumlah_varians += $varians[$d];
			}
			
			$jml_data_per_responden = 0;
			for($f = 0; $f < count($_POST['data'][0]); $f++)
			{
				for($e = 0; $e < count($_POST['data']); $e++)
				{
					$jml_data_per_responden += $_POST['data'][$e][$f];
				}
				$jml_data_per_responden1[$f] =  $jml_data_per_responden;
				$jml_data_per_responden = 0;
			}
			
			$jml_semua_data_per_responden = 0;
			$jml_semua_data_per_responden_kuadrat = 0;
			for($g = 0; $g < count($jml_data_per_responden1); $g++)
			{
				$jml_semua_data_per_responden += $jml_data_per_responden1[$g];
				$jml_semua_data_per_responden_kuadrat += pow($jml_data_per_responden1[$g],2);
			}
			
			$varians_total = ($jml_semua_data_per_responden_kuadrat-(pow($jml_semua_data_per_responden,2)/count($_POST['data'][0])))/count($_POST['data'][0]);
			
			$reliabel = (count($_POST['data'])/(count($_POST['data'])-1))*(1-($jumlah_varians/$varians_total));
			$standard_reliabel = 0.6;
			if($reliabel > $standard_reliabel){
			$this->uji_model->tanda_reliabel($_POST['id_kuesioner'], 1);
			echo 'Data anda sudah reliabel dengan nilai reliabilitas lebih dari 0.6, yaitu sebesar '.$reliabel;
			} else
				{
					$this->uji_model->tanda_reliabel($_POST['id_kuesioner'], 0);
					echo 'Data anda tidak reliabel dengan nilai reliabilitas kurang dari 0.6, yaitu sebesar '.$reliabel;
				}
		} else {
				echo 'data responden kurang!';
			}
	} 
	
	function tanda_reliabel($status)
	{
		$stat = (int) $status;
		$this->uji_model->tanda_reliabel($_POST['id_kuesioner'], $stat);
	}
	
	function tanda_valid($status)
	{
		$stat = (int) $status;
		$this->uji_model->tanda_valid($_POST['id_kuesioner'], $stat);
		//var_dump($status);
		//var_dump($_POST['id_kuesioner']);
	}
	
	function validitas()
	{
		if(count($_POST['data'][0]) > 1)
		{
		
			$jumlah_korelasi_semua = 0;
			$h = 0;
			$jml_data_per_responden = 0;
			$df_min_2 = 0;
			$html = '';
			
			$df_min_2 = count($_POST['data'][0]) - 2;
			if($df_min_2 > 30 && $df_min_2 < 40)
			{
				$df_min_2 = 30;
			} else
				{
					if($df_min_2 > 40 && $df_min_2 < 60)
					{
						$df_min_2 = 40;
					} else
						{
							if($df_min_2 > 60 && $df_min_2 < 80)
							{
								$df_min_2 = 60;
							} else 
								{
									if($df_min_2 > 80 && $df_min_2 < 100)
									{
										$df_min_2 = 80;
									} else 
										{
											if($df_min_2 > 100 && $df_min_2 < 1000)
											{
												$df_min_2 = 100;
											} else 
												{
													if($df_min_2 > 1000)
													{
														$df_min_2 = 1000;
													} 
												} 
										}
								}
						}
				}
			$nilai_t = $this->uji_model->nilai_t($df_min_2);
			//var_dump($nilai_t);
			//var_dump($df_min_2);
			$r_tabel = $nilai_t[0]['0.05']/sqrt($df_min_2+pow($nilai_t[0]['0.05'],2));

			for($f = 0; $f < count($_POST['data'][0]); $f++)
				{
					for($e = 0; $e < count($_POST['data']); $e++)
					{
						$jml_data_per_responden += $_POST['data'][$e][$f];
					}
					$jml_data_per_responden1[$f] =  $jml_data_per_responden;
					$jml_data_per_responden = 0;
				}
			
			for($c = 0; $c < count($_POST['data']); $c++)
			{
				$jumlah_korelasi_XY = 0;
				for($d = 0; $d < count($_POST['data']); $d++){
					//$jumlah_korelasi_XY += pow($this->korelasi->Correlation($_POST['data'][$c], $_POST['data'][$d]),2);
					$korelasi_tiap_variabel[$c][$h++] = $this->korelasi->Correlation($_POST['data'][$c], $_POST['data'][$d]);
				}
				$h = 0;
				$jumlah_korelasi_semua += $jumlah_korelasi_XY;				
			}

			for($g = 0; $g < count($_POST['data']); $g++)
			{
				$korelasi_thdp_nilaitotal[$g] = $this->korelasi->Correlation($_POST['data'][$g], $jml_data_per_responden1);
			}
			//var_dump($korelasi_tiap_variabel);
			$data['validitas_nilai_total'] = $korelasi_thdp_nilaitotal;
			$data['korelasi_tiap_variabel'] = $korelasi_tiap_variabel;
			$data['tabel_r'] = 0.5; //$r_tabel;
			
			foreach($data['validitas_nilai_total'] as $validitas){
				if($validitas < $r_tabel){
					$keterangan[] = 'tidak_valid';
				} else{
					$keterangan[] = 'valid';
					}
			}
			
			if(in_array('tidak_valid', $keterangan))
			{
				$this->uji_model->tanda_valid($_POST['id_kuesioner'], 0);
			} else
				{
					$this->uji_model->tanda_valid($_POST['id_kuesioner'], 1);
				}
			echo json_encode($data);

		} else {
			echo 'Data Responden Kurang!';
			}	
	}
	
	function rincian_validitas($data)
	{
		//$this->load->view('rincian_validitas',$data);
	}
	
	function normalitas()
	{
		if(count($_POST['data'][0]) > 1)
		{
			$jml_item = 0;
			$jml_kuadrat = 0;

			for($a = 0; $a < count($_POST['data']); $a++)
			{
				for($b = 0; $b < count($_POST['data'][$a]); $b++)
				{
					$jml_item += $_POST['data'][$a][$b];
					$jml_kuadrat += pow($_POST['data'][$a][$b],2);
				}
				$jml_item1[$a] = $jml_item;
				$jml_kuadrat1[$a] = $jml_kuadrat;
				$jml_item = 0;
				$jml_kuadrat = 0;
			}
			
			for($c = 0; $c < count($_POST['data']); $c++)
			{
					$varians[$c] = ($jml_kuadrat1[$c]-(pow($jml_item1[$c],2)/count($_POST['data'][0])))/count($_POST['data'][0]);
			}

			
			for($a = 0; $a < count($_POST['data']); $a++)
			{
				//sorted_data = $_POST['data'][$a]
				sort($_POST['data'][$a]);
				$rata_rata = 0;
				$jumlah_data_per_item = 0;
				$standard_deviasi = sqrt($varians[$a]);
				if($standard_deviasi == 0){
					break;
				}
				
				for($b = 0; $b < count($_POST['data'][$a]); $b++)
				{
					$jumlah_data_per_item += $_POST['data'][$a][$b];
				}
				
				$rata_rata = $jumlah_data_per_item/count($_POST['data'][$a]);
				
				$sd[0] = array_unique($_POST['data'][$a]);
				$sd1[0] = array_values($sd[0]);
				//var_dump($sd1[0][1]);
				
				
				
				for($b = 0; $b < count($sd1[0]); $b++)
				{
					//var_dump($sd1[0][$b]);
					//var_dump($rata_rata);
					//var_dump($standard_deviasi);
					//var_dump(($_POST['data'][$a][$b]-$rata_rata)/$standard_deviasi);
					$z = ($sd1[0][$b]-$rata_rata)/$standard_deviasi;
					$z_baru = number_format($z,1);
					$nilai_tabel_normal = $this->uji_model->get_nilai_tabel_normal($z_baru);
					$sz =  $sd1[0][$b]/count($_POST['data'][0]);
					//var_dump($z);
					//var_dump($nilai_tabel_normal[0]['0.05']);
					$nilai = $sz-$nilai_tabel_normal[0]['0.05'];
					$hasil[] = $nilai;
					//var_dump($hasil);
				}
				//var_dump($hasil);
				$hasil_akhir[] = max($hasil);
				$hasil = array();
					//var_dump($hasil_akhir);
					//var_dump($sd[0]);
			}
			
			if(count($_POST['data'][0]) < 4 ){
				$jml_sampel = 0;
			} else {
					if(count($_POST['data'][0]) > 20 && count($_POST['data'][0]) < 26)
					{
						$jml_sampel = 25;
					} else{
							if(count($_POST['data'][0]) > 25 && count($_POST['data'][0]) < 31)
							{
								$jml_sampel = 30;
							} else{
									if(count($_POST['data'][0]) > 30)
									{
										$jml_sampel = 31;
									} else{
										$jml_sampel = count($_POST['data'][0]);
										}
								}
						}
				}
				
			if($jml_sampel == 0){
				$data['tabel_L'] = 0;
			} else{
					//var_dump($jml_sampel);
					$nilai_L = $this->uji_model->get_nilai_tabel_lilifors($jml_sampel);
					$data['tabel_L'] = $nilai_L[0]['0.05'];
				}
				
			$data['nilai_L_hitung'] = $hasil_akhir;
			echo json_encode($data);
			//var_dump($data);
			
		
		} else{
			
			$alerts[] = array('error', 'Data responden kurang!');
			$this->session->set_flashdata('alerts', $alerts);
			
			}
	
	}
	
	
	function uji_data_csv()
	{
		//var_dump($this->input->post('file_csv'));
		$row = 0;
		if(!empty($_FILES['file_csv']['tmp_name'][0])) 
		{
			if($uploaded = $this->do_upload_csv('file_csv')) 
			{
				//var_dump($uploaded);
				$id_data = $this->uji_model->insert_file_csv($this->input->post('nama_data'), $this->input->post('deskripsi_data'), $uploaded['file_name'], $this->session->userdata('id_user'), $this->input->post('pp'), $this->input->post('pm'), $this->input->post('pd'));								
				//$this->kuesioner_model->update_file($uploaded['file_name'], $id_kuesioner);
				//var_dump($uploaded);
				
				
			} 
			
		} 
		
		if(!empty($_FILES['file']['tmp_name'][0])) 
		{
			if($uploaded1 = $this->do_upload('file')) 
			{
				//var_dump($uploaded);
				$this->uji_model->update_proposal($id_data, $uploaded1['file_name']);								
				//$this->kuesioner_model->update_file($uploaded['file_name'], $id_kuesioner);
				//var_dump($uploaded);
				
				
			} 
			
		} 
		$data = array();
		$data_csv = $this->uji_model->get_data_csv($id_data);
		//var_dump($data_csv);
		$file = fopen(base_url().'file/data_csv/'.$data_csv[0]['file_csv'],"r");
					//var_dump(base_url().'file/data_csv/'.$uploaded['file_name']);
					while(! feof($file))
					  {
						  $data[$row] = (fgetcsv($file));
						  $row++;
					  }
		fclose($file);
		//array_pop($data);
		//var_dump($data);
			$id_responden = '';
					for($b = 0; $b < count($data); $b++)
					{
						$id_responden = $this->uji_model->insert_responden('responden');
						
						  for($a = 0; $a < count($data[$b]); $a++)
						  {
								if(!empty($data[$b])){
									if($b == 0)
									{
										//var_dump($data[$b][$a]);
										$variabel[$a] = $this->uji_model->insert_variabel_csv($data[$b][$a], $id_data, 3);
										
									} else
										{
											$id_jawaban_responden = $this->uji_model->insert_jawaban_responden($data[$b][$a]);
											$this->uji_model->insert_responden_kuesioner($id_responden, $variabel[$a],$id_jawaban_responden);
										}
							  }
						  }
						  
					}
				
				//var_dump($variabel);
			redirect('uji/data/'.$id_data);
		
	}
	
	function do_upload_csv($field)
	{
		$this->load->helper('file');
		
		$dir = 'file/data_csv/';
		if(!is_dir($dir)) {
			mkdir($dir, 0777, true);
		}
				
		$config['upload_path'] = $dir;
		$config['encrypt_name'] = true;
		$config['remove_spaces'] = true;
		$config['allowed_types'] = 'csv';
		
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if($this->upload->do_upload($field)) {
			$list_uploaded = $this->upload->data();			
			return $list_uploaded;
		}
		else{
		}
	}
	
	function do_upload($field)
	{
		$this->load->helper('file');
		
		$dir = 'file/proposal/';
		if(!is_dir($dir)) {
			mkdir($dir, 0777, true);
		}
				
		$config['upload_path'] = $dir;
		$config['encrypt_name'] = true;
		$config['remove_spaces'] = true;
		$config['allowed_types'] = 'pdf';
		
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if($this->upload->do_upload($field)) {
			$list_uploaded = $this->upload->data();			
			return $list_uploaded;
		}
		else{
		}
	}
	
	function get_keterangan($id)
	{
		$data['data'] = $this->uji_model->get_keterangan($id);
		//var_dump($data['data'][0]['publish']);
		echo json_encode($data);
	}
	
	function update_keterangan()
	{
		$this->uji_model->update_keterangan($_POST['id_kuesioner'], $_POST['pp'], $_POST['pm'], $_POST['pd']);
	}
	
	
	
}
//$korelasi_max = max($korelasi_tiap_variabel);
		//var_dump($korelasi_tiap_variabel);
		//var_dump($korelasi_max);
		//var_dump($jumlah_korelasi_semua);
		
		/*for($h = 0; $h < count($_POST['data'][0]); $h++)
		{
			$korelasi_max_array[$h] = sort($korelasi_tiap_variabel);
		}*/
		
		//adis
		//28RIFDA07setya92muna!!
		
		/*$jumlah_korelasi_parsial_semua = 0;
		for($f = 0; $f < count($_POST['data']); $f++)
		{
			$g = $f+1;
			$korelasi_parsialXY_Z = 0;
			for($g; $g < count($_POST['data']); $g++){
				$korelasi_XY = $this->korelasi->Correlation($_POST['data'][$f], $_POST['data'][$g]);
				$korelasi_XZ = $this->korelasi->Correlation($_POST['data'][$f], $_POST['data'][0]);
				$korelasi_YZ = $this->korelasi->Correlation($_POST['data'][$g], $_POST['data'][0]);
				
				$korelasi_parsialXY_Z = ($korelasi_XY - ($korelasi_XZ*$korelasi_YZ))/(sqrt(1-pow($korelasi_XZ,2))*sqrt(1-pow($korelasi_YZ,2)));
				$jumlah_korelasi_parsialXY_Z += pow($korelasi_parsialXY_Z,2);
				//var_dump($korelasi_parsialXY_Z);
				//var_dump($korelasi_XY);
				var_dump($korelasi_XZ);
				var_dump($korelasi_YZ);
				
			}
			//$jumlah_korelasi_parsial_semua += $jumlah_korelasi_parsialXY_Z;
		//var_dump($jumlah_korelasi_parsial_semua);
		
		}*/
		//var_dump($_POST['data'][0]);
		//var_dump($korelasi_max_array);
		//var_dump($jumlah_korelasi_semua);
		//var_dump($jumlah_korelasi_parsial_semua);
		//$KMO = $jumlah_korelasi_semua /($jumlah_korelasi_semua- $jumlah_korelasi_parsial_semua);//$jumlah_korelasi_parsial_semua);
		//var_dump($KMO);
?>

